﻿using IAD13.fromTDDtoBDD.Exceptions;

namespace IAD13.fromTDDtoBDD
{
    public class Account : IAccount
    {
        private readonly IAmount balance;

        public Account(IAmount balance)
        {
            this.balance = balance;
        }
        
        public Account(decimal balance, ICurrency currency) 
            : this(new Amount(balance,currency))
        {
        }

        public IAmount Balance()
        {
            return balance;
        }

        public void Deposit(IAmount amount)
        {
            CheckCurrency(amount);
            IncreaseBalance(amount);
        }

        private void IncreaseBalance(IAmount amount)
        {
            balance.Value += amount.Value;
        }

        public void Withdrawal(IAmount amount)
        {
            CheckCurrency(amount);
            CheckBalanceForWithdrawal(amount);
            DecreaseBalance(amount);
        }

        private void DecreaseBalance(IAmount amount)
        {
            balance.Value -= amount.Value;
        }

        private void CheckCurrency(IAmount amount)
        {
            if (!amount.Currency.Equals(balance.Currency))
                throw new CurrencyIsWrongException();
        }
        
        private void CheckBalanceForWithdrawal(IAmount amount)
        {
            if (balance.Value < amount.Value)
                throw new WithdrawalGreaterThanBalanceException();
        }
    }
}